#include "iTimerThread.h"
#include "../h/iLogger.h"

int iTimerThread::on_message(iMsgID msg, void* param)
{
	switch(msg)
	{
	case MSG_ADD_TIMER:
		m_timer_wheel.add_timer((iTimer*)param, ((iTimer*)param)->get_interval()>>4);
		break;
	default:
		return iMsgThread::on_message(msg,param);
	}

	return 0;
}
int iTimerThread::run()
{
#if defined(__WIN__)
	SOCKET fd = socket(AF_INET, SOCK_STREAM, 0);
#endif
	while(m_runing)
	{
		iMsg* msg = get_message();
		if(NULL == msg)
		{
			static int test=0;

			fd_set read_fds;
			FD_ZERO(&read_fds);
			struct timeval tv;
			tv.tv_sec = 0;
			tv.tv_usec = 8000;
			#if defined(__WIN__)
			FD_SET(fd, &read_fds);
			if(::select(fd+1, &read_fds, NULL, NULL, &tv))
			#elif defined(__LINUX__)
			if(::select(0, NULL, NULL, NULL, &tv))
			#endif
			{
      			printf("select failed\n");
			}
			
/*
			SYSTEMTIME sys;
			GetLocalTime( &sys );
			printf("[%4d-%02d-%02d %02d:%02d:%02d.%03d]\n",	sys.wYear, 
															sys.wMonth, 
															sys.wDay, 
															sys.wHour, 
															sys.wMinute, 
															sys.wSecond, 
															sys.wMilliseconds);
			test++;
			if(test==100) system("pause");*/
			m_timer_wheel.jiffy();
		}
		else
		{
			msg->process();
		}
	}

	return 0;
}
